//
//  JPictureSelectorCollectionVC.swift
//  JMicroBlog
//
//  Created by Jason on 28/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit
class JPictureSelectorCollectionVC: UICollectionViewController {
    // 图片数组
    var pictureList = [UIImage]()
    override func viewDidLoad() {
        super.viewDidLoad()
        regNotification()
    }
    
    // 注册通知
    private func regNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectPicture:", name: JPictureSelectNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "removePicture:", name: JPictureRemoveNotification, object: nil)
    }
    
    // 选择图片
    func selectPicture(noti:NSNotification) {
        // 取出通知中的cell
        let cell = noti.object as! JPictureSelectorCell
        if cell.image != nil {
            // 已经设置了图片
            return
        }
        // case PhotoLibrary  照片库 除了保存的相册还有同步后的照片
        // case Camera         照相机
        // case SavedPhotosAlbum  从软件保存的照片
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            JLog.i("不支持")
            return
        }
        // 访问照片 并实例化
        let imgPicker = UIImagePickerController()
        // 成为imgPicker的代理对象
        imgPicker.delegate = self
//        imgPicker.allowsEditing = true
        // 4. 指定类型
        imgPicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        presentViewController(imgPicker, animated: true, completion: nil)
    }
    
    // 删除图片
    func removePicture(noti:NSNotification) {
        // 将照片从数组中删除
        // 1.从通知获取cell
        let cell = noti.object as! JPictureSelectorCell
        // 2.判断cell的indexPath
        if let indexPath = collectionView?.indexPathForCell(cell) {
            // 3.根据indexPath将指定cell从数组中移除
            pictureList.removeAtIndex(indexPath.item)
            
            collectionView?.reloadData()
        }
    }
    
    // 删除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList.count + 1
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("JPictureSelectorCell", forIndexPath: indexPath) as! JPictureSelectorCell
        // 判断 index.item是否是最后一项 最后一项会越界
        if indexPath.item < pictureList.count {
            cell.image = pictureList[indexPath.item]
        } else {
            // 恢复显示背景图片
            cell.image = nil
            
        }
        return cell
    }
}

extension JPictureSelectorCollectionVC : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    /**
    照片加载完成的操作
    
    :param: picker      哪个picker
    :param: image       哪张图片
    :param: editingInfo 编辑信息
    一旦实现了该方法 就需要自己来dismiss
    */
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        // 将用户选择的照片添加到图像数组
        pictureList.append(image.scaleImage(width: 320)!)
        // 重新刷新collectionView
        collectionView?.reloadData()
        // 关闭控制器
        dismissViewControllerAnimated(true, completion: nil)
    }
}
